package com.example.membersteward.service.impl;

import com.example.membersteward.entity.User;
import com.example.membersteward.mapper.UserMapper;
import com.example.membersteward.util.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    private static final Logger logger = LoggerFactory.getLogger(UserDetailsServiceImpl.class);
    @Resource
    UserMapper userMapper;

    @Override
    public User loadUserByUsername(String username) throws UsernameNotFoundException {
        logger.info("执行用户查询");
        User user = userMapper.selectByName(username);
        if (user == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        return user;

    }

    public User loadUserByUserId(Long userId) throws UsernameNotFoundException {

        User user = userMapper.selectByUserId(userId);
        if (user == null) {
            throw new UsernameNotFoundException("用户不存在");
        }

        // 将用户的角色转换为 GrantedAuthority 对象的集合
//        List<GrantedAuthority> authorities = new ArrayList<>();
//        for (String role : user.get()) {
//            authorities.add(new SimpleGrantedAuthority("ROLE_" + role));
//        }

        return user;
    }
}